import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../res/style.dart';

/// @description :公共加载弹窗
class LoadingDialog extends StatefulWidget {
  String text = '';
  int dialogTyle = 1; //1 黑白圈圈对话框， 2 白底橙色框

  LoadingDialog({
    Key? key,
    this.text = "加载中...",
    this.dialogTyle = 1,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => LoadingDialogState();
}

class LoadingDialogState extends State<LoadingDialog>
    with SingleTickerProviderStateMixin {
  late AnimationController _animController;

  @override
  void initState() {
    super.initState();
    _animController =
        AnimationController(vsync: this, duration: const Duration(seconds: 1));
    _animController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        //创建透明层
        type: MaterialType.transparency, //透明类型
        child: Center(
          //保证控件居中效果
          child: SizedBox(
            width: widget.dialogTyle == 1 ? 160.w : 270.w,
            height: widget.dialogTyle == 1 ? 103.h : 128.h,
            child: Container(
              decoration: ShapeDecoration(
                color: widget.dialogTyle == 1 ? Colors.black54 : Colors.white,
                shape: const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(20.0),
                  ),
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Center(
                    child: RotationTransition(
                      turns: _animController
                        ..addStatusListener((status) {
                          if (status == AnimationStatus.completed) {
                            _animController.reset();
                            _animController.forward();
                          }
                        }),
                      child: Image.asset(
                        widget.dialogTyle == 1
                            ? "assets/images/loading.png"
                            : "assets/images/loading2.png",
                        height: 28.h,
                        width: 28.w,
                      ),
                    ),
                  ),
                  Box.hBox13,
                  Text(widget.text,
                      style: widget.dialogTyle == 1
                          ? Styles.style_white_16_bold
                          : Styles.style_black_16_bold),
                ],
              ),
            ),
          ),
        ));
  }

  @override
  void dispose() {
    _animController.dispose(); //解决内存泄漏
    super.dispose();
  }
}
